#ifndef SERIALPORT_H
#define SERIALPORT_H

#include <string>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <fstream>

#include "device.h"

namespace SerialPortInterface {
  enum BaudRate {
    BaudRate50,
    BaudRate75,
    BaudRate110,
    BaudRate134,
    BaudRate150,
    BaudRate200,
    BaudRate300,
    BaudRate600,
    BaudRate1200,
    BaudRate1800,
    BaudRate2400,
    BaudRate4800,
    BaudRate9600,
    BaudRate19200,
    BaudRate38400,
    BaudRate57600,
    BaudRate115200
  };
  
  enum DataBits {
    DataBits5,
    DataBits6,
    DataBits7,
    DataBits8
  };
  
  enum StopBits {
    StopBits1,
    StopBits2
  };
  
  enum Parity {
    ParityNone,
    ParityOdd,
    ParityEven
  };
  
  class SerialPort {
  public:
    SerialPort(
	       Device device,
	       BaudRate _baudrate = BaudRate57600,
	       DataBits _databits = DataBits8,
	       StopBits _stopbits = StopBits1,
	       Parity _parity = ParityNone
	       );
    ~SerialPort();
 
    inline Device GetDevice() { return device; }
   
    char getChar();
    std::string getString(int length);
    void sendChar(char x);
    void sendString(std::string str);
    void sendFile(std::string filename);

    void setBaudRate(BaudRate BR);
    void setDataBits(DataBits DB);
    void setStopBits(StopBits SB);
    void setParity(Parity P);

    BaudRate getBaudRate();
    DataBits getDataBits();
    StopBits getStopBits();
    Parity getParity();
  private:
    int fd;
    struct termios OldOptions; 

    BaudRate baudrate;
    DataBits databits;
    StopBits stopbits;
    Parity parity;
    
    Device device;
  };
}

#endif
